/*
* 选择安全问题条例
* */

import 'package:center_beijing/api/task_api.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/widgets/search_widget.dart';
import 'package:center_beijing/widgets/src/tree_node.dart';
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class SelectSafetyOrdinance extends StatefulWidget {
  @override
  _SelectSafetyOrdinanceState createState() => _SelectSafetyOrdinanceState();
}

class _SelectSafetyOrdinanceState extends State<SelectSafetyOrdinance> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  TextEditingController _textInput = TextEditingController();
  List nodeList = ['消防设施、临时用电等日常安全', '消防设施数量不足或不满足消防', '禁火区域吸烟或违章使用火'];
  int currentIndex;
  var currentSelect = '';

  List allNodes = [];

  var selectElement;

  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
    this.getIssue();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  /*
  * 获取引用标准
  * */
  getIssue() async {
    var result = await getQsSafeIssue({
      'projectSectionId': GlobalConfig.selectFactory.tag,
      'issueType': '1',
      'pId': '-1',
      'catalogType': '',
    });
    if (result != null) {
      this.setState(() {
        allNodes = result;
      });
    }
    LogUtil.d(result);
  }

  getIssue2(element) async {
    var result = await getQsSafeIssue({
      'projectSectionId': GlobalConfig.selectFactory.tag,
      'issueType': '1',
      'pId': element['id'],
      'catalogType': element['tag'],
    });
    if (result != null) {
      return result;
    }
    else{
      return [];
    }
  }

  selectItem(items){
    List<Widget> tem = [];
    if(items!=null&&items.length>0){
      items.forEach((element) {
        tem.add(TreeNode(
          trailing:element['isParent']?null:Image.asset(element['checked']?'assets/images/now_gou.png':'assets/images/quan.png',width: 15.w,height: 15.h,),
          titleOnTap: ()async{
            if(!element['isParent']){
              this.setState(() {
                if(selectElement!=element){
                  element['checked'] = true;
                  if(selectElement!=null){
                    selectElement['checked'] = false;
                  }
                  selectElement = element;
                }else{
                  element['checked'] = !element['checked'];
                }
              });
            }else{
              if(element['child']==null){
                List result = await this.getIssue2(element);
                if(result.length>0){
                  this.setState(() {
                    element['child'] = result;
                  });
                }
              }
            }
          },
          trailingOnTap: () async{
            if(!element['isParent']){
              this.setState(() {
                if(selectElement!=element){
                  element['checked'] = true;
                  if(selectElement!=null){
                    selectElement['checked'] = false;
                  }
                  selectElement = element;
                }else{
                  element['checked'] = !element['checked'];
                }
              });
            }else{
              if(element['child']==null){
                List result = await this.getIssue2(element);
                if(result.length>0){
                  this.setState(() {
                    element['child'] = result;
                  });
                }
              }
            }
          },
          title: Text(element['name']),
          children: this.selectItem(element['child']),
        ));
      });
    }
    return tem;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('条例选择'),
        ),
        body: Container(
          color: Styles.MainBackgroundColor,
          padding: EdgeInsets.fromLTRB(
              0, 0, 0, MediaQuery.of(context).padding.bottom + 70),
          child: SingleChildScrollView(
            child: Column(
              children:allNodes.length>0? this.selectItem(allNodes):[],
            ),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: Container(
          padding: EdgeInsets.fromLTRB(
              Styles.spacing8, Styles.spacing8, Styles.spacing8, 10),
          child: Container(
            decoration: new BoxDecoration(
              //背景
              color: Styles.BlueColor,
              //设置四周圆角 角度
              borderRadius: BorderRadius.all(Radius.circular(12.0)),
            ),
            child: FlatButton(
              disabledColor: Styles.SelectColor,
              onPressed: () {
                Get.back(result: this.selectElement);
              },
              child: Container(
                height: Styles.MainButtonHeight,
                alignment: Alignment.center,
                child: Text(
                  '确定',
                  style: TextStyle(
                      fontSize: Styles.MainButtonTextFont, color: Colors.white),
                ),
              ),
            ),
          ),
        ));
  }
}
